<template>
  <div class="login">
    <div class="mack" @click="changeIsShowLoginModal(false)"></div>
    <div class="popup">
      <div class="close" @click="changeIsShowLoginModal(false)"></div>
      <section class="nav">
        <a
          href="Javascript:;"
          :class="{ active: isShow }"
          @click="isShow = true"
          >手机号码登录</a
        >|<a
          href="Javascript:;"
          @click="showQrCode"
          :class="{ active: !isShow }"
          >微信扫码登录</a
        >
      </section>
      <div class="conten">
        <div v-show="isShow" class="phonebox">
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机号"
              v-model="phoneNumber"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
              :imgs="imgsarr"
            ></slide-verify>
          </div>
          <div class="row">
            <input
              type="text"
              placeholder="请输入短信验证码"
              v-model="logincodes"
            /><button class="btncode" @click="getcode" v-show="isShowphone">
              获取验证码
            </button>
            <button class="btncode" v-show="!isShowphone">
              {{ count }}
            </button>
          </div>
          <div class="row">
            <button class="btnlogin" @click="record">登录</button>
          </div>
        </div>
        <div v-show="!isShow" id="weixin">微信登录</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations } from "vuex";
import { validatePhoneNumber } from "@/utils";
import { logincode, phoneloginAPI, bangdingAPI } from "@/request/api";
export default {
  data() {
    return {
      isShow: true,
      msg: "向右滑动",
      imgsarr: [
        require("../assets/img/integral-01.9386d4bf.png"),
        require("../assets/img/integral-02.150d92a1.png"),
        require("../assets/img/integral-03.9870f3f1.png"),
        require("../assets/img/integral-04.afadcbdf.png"),
      ],
      phoneNumber: "",
      count: 5,
      isShowphone: true,
      logincodes: "",
    };
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsShowLogin: "showLogin/changeIsShowLogin",
      // changeisShowToast: "showToast/changeisShowToast",
    }),
    ...mapActions({
      asyncisshowtoast: "showToast/asyncisshowtoast",
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    toVerifyy() {
      if (!validatePhoneNumber(this.phoneNumber)) {
        this.asyncisshowtoast({
          isshow: true,
          msg: "请输入正确的手机号",
          type: "danger",
        });
        return;
      }
      if (this.msg == "向右滑动" || this.msg == "再来一次") {
        this.asyncisshowtoast({
          isshow: true,
          msg: "请滑动验证码",
          type: "danger",
        });
        return;
      }
      return true;
    },
    async getcode() {
      if (!this.toVerifyy()) {
        return;
      }
      let res = await logincode({
        phone: this.phoneNumber,
      });
      if (!res) return;
      if (res.code == 0) {
        this.isShowphone = false;
        let times = setInterval(() => {
          this.count--;
          if (this.count == 0) {
            clearInterval(times);
            this.count = 5;
            this.isShowphone = true;
          }
        }, 1000);
      }
    },
    async record() {
      //前面条件是否通过
      if (!this.toVerifyy()) return;
      //判断验证码不为空
      console.log(this.logincodes);
      if (this.logincodes == "") {
        this.asyncisshowtoast({
          isshow: true,
          msg: "请输入验证码再进行登录",
          type: "danger",
        });
        return;
      }
      let uuid = localStorage.getItem("uuid");
      let res = null;
      if (uuid) {
        //判断微信是否第一次登录,是就执行绑定
        res = await bangdingAPI({
          verifyCode: this.logincodes,
          phone: this.phoneNumber,
          uuid,
        });
        console.log("uuid");
      } else {
        //前面条件都通过则发送手机登录请求
        res = await phoneloginAPI({
          verifyCode: this.logincodes,
          phone: this.phoneNumber,
        });
        console.log("sjh");
      }
      if (!res) return;
      localStorage.setItem("x-auth-token", res["x-auth-token"]);
      this.changeIsShowLoginModal(false);
      this.changeIsShowLogin(true);
      localStorage.removeItem("uuid");
    },
    showQrCode() {
      this.isShow = false;
      //获取二维码 并展示
      // 申请微信登录二维码
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlLA0KLmltcG93ZXJCb3ggLmluZm8gew0KICAgIGRpc3BsYXk6IG5vbmU7DQp9",
      });
    },
  },
};
</script>

<style lang='less' scoped>
.login {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  .mack {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
  }
  .popup {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    background-color: #cfc;
    width: 555px;
    height: 423px;
    background: url("../assets/img/login-box-bg.png");
    .close {
      position: absolute;
      right: 60px;
      top: 15px;
      width: 22px;
      height: 22px;
      background: url("../assets/img/close.png");
    }
  }
  .nav {
    text-align: center;
    font-size: 20px;
    margin-top: 40px;
    color: #cccccc;
    a {
      text-decoration: none;
      color: #cccccc;
      padding: 0 10px;
      &.active {
        color: #333;
      }
    }
  }
  .conten {
    width: 355px;
    margin: 10px auto 0;
    text-align: center;
    .phonebox {
      margin-top: 20px;
      .row {
        display: flex;
        margin-bottom: 20px;
        input {
          flex: 1;
          height: 50px;
          text-indent: 2em;
          outline: none;
        }
        button {
          background-color: #0a328e;
          color: #fff;
          border: none;
        }
        .btncode {
          margin-left: 10px;
          width: 90px;
        }
        .btnlogin {
          height: 50px;
          width: 100%;
        }
      }
    }
  }
  /deep/.slide-box {
    width: 100%;
    position: relative;
    box-sizing: border-box;
    canvas {
      position: absolute;
      left: 0;
      top: 60px;
      display: none;
      width: 100%;
      box-sizing: border-box;
    }
    .slide-verify-block {
      width: 85px;
      height: 136px;
    }
    .slide-verify-refresh-icon {
      top: -120px;
      display: none;
    }
    &:hover {
      canvas {
        display: block;
      }
      .slide-verify-refresh-icon {
        display: block;
      }
    }
  }
}
</style>